package com.cmcc.aregister.ui.adapter;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.constant.HelpInc;
import com.cmcc.aregister.cs.model.Appointment;
import com.cmcc.aregister.cs.model.DataUtilEntity;
import com.cmcc.aregister.cs.util.Utils;

public class LocalOrderInfoAdapter extends BaseAdapter {
	private Context context;
	public List<Appointment> list;
	HelpInc helpInc;

	public LocalOrderInfoAdapter(Context context, List<Appointment> list,HelpInc helpInc) {
		this.context = context;
		this.list = list;
		this.helpInc = helpInc;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.a_order_detail_local, null);
			holder.order_name = (TextView) convertView
                .findViewById(R.id.order_name);
			holder.order_chn_name = (TextView) convertView
                .findViewById(R.id.order_chn_name);
			holder.order_id = (TextView) convertView
					.findViewById(R.id.order_id);
			holder.order_status = (TextView) convertView
					.findViewById(R.id.order_status);
			holder.order_time = (TextView) convertView
					.findViewById(R.id.order_time);
			holder.order_info = (TextView) convertView
					.findViewById(R.id.order_info);
			holder.order_btn = (Button) convertView.findViewById(R.id.order_btn);
			holder.order_btn.setOnClickListener(mOnClickListener);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Appointment app = list.get(position);
		holder.order_name.setText(app.truename);
		holder.order_chn_name.setText(app.chn_name);
		holder.order_id.setText(app.order_no);
		holder.order_status.setText(Utils.getStatus(app.yuyue_state));
		holder.order_time.setText(app.to_date);
		holder.order_info.setText(app.unit_name + "\n" + app.dep_name + " "
				+ app.doctor_name);
		holder.order_btn.setText(Utils.getBtnStatus(app.yuyue_state));
		DataUtilEntity mData = new DataUtilEntity();
		mData.msg = Utils.getBtnStatus(app.yuyue_state);
		mData.position = position;
		holder.order_btn.setTag(mData);
		return convertView;
	}

	static class ViewHolder {
		TextView order_id;
		TextView order_status;
		TextView order_time;
		TextView order_info;
		TextView order_name;
		TextView order_chn_name;
		Button order_btn;
	}
	
	OnClickListener mOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			DataUtilEntity mData = (DataUtilEntity) v.getTag();
			if(mData.msg.equals(context.getString(R.string.again_order))){//再次预约
				helpInc.notifyUI(GlobalSetting.AGIAN_ORDER,mData.position);
			}else{//取消预约
				helpInc.notifyUI(GlobalSetting.CANCEL_ORDER,mData.position);
			}
		}
	};
}